﻿package dut.ir.lxb.weibo.spider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import dut.ir.lxb.weibo.bean.EventBean;
import dut.ir.lxb.weibo.bean.UserBean;
import dut.ir.lxb.weibo.bean.UserWeiboAction;
import dut.ir.lxb.weibo.bean.WeiboBean;
import dut.ir.lxb.weibo.util.ActionType;
import dut.ir.lxb.weibo.util.SinaSSOEncoder;
import dut.ir.lxb.weibo.util.SpiderConfig;

/**
 * 解析个人信息
 * @author Baogege
 *
 */
public class WeiboParser
{
	
	/**
	 * 解析页数
	 * @param respStr json字符串
	 * @return 页数
	 */
	public int getPageCount(String respStr)
	{
		int pageCount = 0;
		if(respStr == null || respStr.trim().equals("")) return 0;
		try
		{
			JSONObject jsonObject = new JSONObject(respStr);
			pageCount = jsonObject.getJSONObject("data").getJSONObject("page").getInt("totalpage");
			
		}
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return pageCount;
	}
	
	/**
	 * 通过原始页面解析微博
	 * @param page 页面字符串
	 * @param ub 用户信息
	 * @return 返回微博
	 */
	public WeiboBean parseWeiboByUrlPage(String page, UserBean ub)
	{
		Document doc = Jsoup.parse(page);
		Elements scripts = doc.getElementsByTag("script");
		if(scripts == null || scripts.size() == 0)
			return null;
		String dataStr = "";
		for(Element cs : scripts)
		{
			String currStr = cs.toString();
			if(currStr.contains("FM.view({\"ns\":\"pl.content.weiboDetail.index"))
			{
				dataStr = currStr;
				break;
			}
		}
		if(dataStr.equals(""))
			return null;
		dataStr = dataStr.substring(dataStr.indexOf('(') + 1, dataStr.lastIndexOf(')'));
		try
		{
			JSONObject jsonObject = new JSONObject(dataStr);
			dataStr = jsonObject.getString("html");
			Document dataDocument = Jsoup.parse(dataStr);
			//System.out.println(dataDocument.toString());
			Element weiboElement = null;
			if(dataDocument.getElementsByAttributeValue("action-type", "feed_list_item") != null && dataDocument.getElementsByAttributeValue("action-type", "feed_list_item").size() > 0)
				weiboElement = dataDocument.getElementsByAttributeValue("action-type", "feed_list_item").get(0);
			WeiboBean wb = new WeiboBean();
			
			String mid = weiboElement.attr("mid");
			wb.setMid(mid);
			String content = weiboElement.getElementsByTag("em").get(0).ownText();
			wb.setContent(content);
			wb.setNickname(ub.getNickName());
			
			
			
			
			return wb;
			
		}
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 解析用户关注列表
	 * @param respStr
	 * @return
	 */
	public List<String> parseFollowList(String respStr)
	{
		List<String> followList = new ArrayList<String>();
		Document doc = Jsoup.parse(respStr);
		String dataStr = "";
		Elements scripts = doc.getElementsByTag("script");
		for(Element currentScript : scripts)
		{
			String currentStr = currentScript.toString().trim();
			if(currentStr.contains("FM.view({\"ns\":\"pl.content.followTab.index"))
			{
				dataStr = currentStr;
				break;
			}
		}
		
		try
		{
			dataStr = dataStr.substring(dataStr.indexOf('(') + 1, dataStr.lastIndexOf(')'));
			JSONObject jsonObject = new JSONObject(dataStr);
			dataStr = jsonObject.getString("html");
			doc = Jsoup.parse(dataStr);
			Elements followElements = doc.getElementsByAttributeValue("action-type", "itemClick");
			for(Element currentFollow : followElements)
			{
				String uid = currentFollow.attr("action-data");
				uid = uid.substring(uid.indexOf('=') + 1, uid.indexOf('=') + 11);
				followList.add(uid);
			}
			
			//System.out.println(doc.toString());
		} 
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return followList;
	}
	
	/**
	 * 从Response字符串中解析
	 * @param dataStr
	 * @param wb
	 * @param actionType
	 * @return
	 */
	public List<UserWeiboAction> parseUserWeiboActionList(String dataStr, WeiboBean wb, ActionType actionType)
	{
		List<UserWeiboAction> uwActionList = null;
		if(dataStr == null) return null;
		try
		{
			JSONObject jsonObject = new JSONObject(dataStr);
			String htmlStr = jsonObject.getJSONObject("data").getString("html");
			Document doc = Jsoup.parse(htmlStr);
			//System.out.println(doc.toString());
			//转发
			if(actionType.equals(ActionType.FORWARD))
			{
				uwActionList = parseForwardList(doc, wb);
			}
			//评论
			else if(actionType.equals(ActionType.COMMENT))
			{
				uwActionList = parseCommentList(doc, wb);
			}
			//点赞
			else if(actionType.equals(ActionType.LIKE))
			{
				uwActionList = parseLikeList(doc, wb);
			}
			
			
		} 
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		
		return uwActionList;
	}
	
	/**
	 * 解析评论列表
	 * @param doc
	 * @param wb
	 * @return
	 */
	public List<UserWeiboAction> parseCommentList(Document doc, WeiboBean wb)
	{
		List<UserWeiboAction> commentList = new ArrayList<UserWeiboAction>();
		
		Elements commentElements = doc.getElementsByAttributeValue("class", "comment_list S_line1");
		for(Element currentComment : commentElements)
		{
			UserWeiboAction uwa = new UserWeiboAction();
			
			Element userElement = currentComment.getElementsByTag("a").get(1);
			String uid = userElement.attr("usercard").trim();
			uid = uid.substring(uid.indexOf('=') + 1);
			String nickname = userElement.attr("title");
			String datetime = currentComment.getElementsByClass("S_txt2").get(0).text().trim();
			datetime = datetime.substring(1, datetime.lastIndexOf(')'));
			String content = currentComment.getElementsByTag("dd").get(0).ownText();
			
			uwa.setActionType(ActionType.COMMENT);
			uwa.setMid(wb.getMid());
			uwa.setOwneruid(wb.getUid());
			uwa.setOwnerNickname(wb.getNickname());
			uwa.setUid(uid);
			uwa.setNickname(nickname);
			uwa.setDatetime(datetime);
			uwa.setContent(content);
			
			commentList.add(uwa);
		}
		
		return commentList;
	}
	
	/**
	 * 解析点赞列表
	 * @param doc
	 * @param wb
	 * @return 点赞列表
	 */
	public List<UserWeiboAction> parseLikeList(Document doc, WeiboBean wb)
	{
		List<UserWeiboAction> likeList = new ArrayList<UserWeiboAction>();
		
		Elements likeElements = doc.getElementsByTag("li");
		for(Element currentLike : likeElements)
		{
			UserWeiboAction uwa = new UserWeiboAction();
			String uid = currentLike.attr("uid");
			String nickname = currentLike.getElementsByTag("img").get(0).attr("title").trim();
			
			uwa.setOwneruid(wb.getUid());
			uwa.setOwnerNickname(wb.getNickname());
			uwa.setMid(wb.getMid());
			uwa.setNickname(nickname);
			uwa.setUid(uid);
			uwa.setActionType(ActionType.LIKE);
			
			likeList.add(uwa);
		}
		
		return likeList;
	}
	
	/**
	 * 解析转发列表
	 * @param doc
	 * @return
	 */
	public List<UserWeiboAction> parseForwardList(Document doc, WeiboBean wb)
	{
		if(doc == null)
			return null;
		List<UserWeiboAction> forwardList = new ArrayList<UserWeiboAction>();
		Elements forwardElements = doc.getElementsByTag("dl");
		for(Element currentForward : forwardElements)
		{
			UserWeiboAction uw = new UserWeiboAction();
			
			//解析转发数据
			Element userElement = currentForward.getElementsByTag("dd").get(0).getElementsByTag("a").get(0);
			String nickname = userElement.attr("nick-name");
			String uid = userElement.attr("usercard");
			uid = uid.substring(uid.indexOf('=') + 1);
			String forwardmid = currentForward.attr("mid");
			String content = currentForward.getElementsByTag("em").get(0).ownText().trim();
			if(content.contains("//"))
				content = content.substring(0, content.indexOf("//"));
			String fullContent = currentForward.getElementsByTag("em").get(0).text().trim();
//			if(fullContent.contains("//"))
//				fullContent = fullContent.substring(0, fullContent.indexOf("//"));
			String dateTime = currentForward.getElementsByAttributeValue("node-type", "feed_list_item_date").get(0).attr("title");
			//Elements pathElements = currentForward.getElementsByTag("em").get(0).getElementsByTag("a");
			String path = "";
			Pattern p = Pattern.compile("//@(.*?):");
			Matcher m = p.matcher(fullContent);
			while(m.find())
			{
				String curPath = "@" + m.group(1);
				path += curPath;
			}
			path = path.trim();
			//System.out.println(path.trim());
//			for(Element currentPath : pathElements)
//			{
//				if(currentPath.text().contains("@"))
//					path += currentPath.text().trim();
//			}
			
			uw.setDatetime(dateTime);
			uw.setActionType(ActionType.FORWARD);
			uw.setPath(path);
			uw.setContent(content);
			uw.setUid(uid);
			uw.setNickname(nickname);
			uw.setMid(wb.getMid());
			uw.setOwneruid(wb.getUid());
			uw.setOwnerNickname(wb.getNickname());
			uw.setForwardmid(forwardmid);
			
			forwardList.add(uw);
		}
		
		return forwardList;
	}
	
	/**
	 * 解析微博列表
	 * @param weiboPage
	 * @param ub 用户信息
	 * @return
	 */
	public List<WeiboBean> parseWeiboList(String weiboPage, UserBean ub)
	{
		List<WeiboBean> weiboList = new ArrayList<WeiboBean>();
		try
		{
			JSONObject jsonObject = new JSONObject(weiboPage);
			String weiboInfo = jsonObject.getString("data");
			Document doc = Jsoup.parse(weiboInfo);
			//System.out.println(doc.toString());
			Elements weiboElements = doc.getElementsByAttributeValue("action-type", "feed_list_item");
			
			for(Element weiboElement : weiboElements)
			{
				WeiboBean currentWeibo = parseWeibo(weiboElement, ub);
				if(currentWeibo.getMid().equals("") || currentWeibo.getMid() == null)
					continue;
				weiboList.add(currentWeibo);
			}
			
			//System.out.println(doc.toString());
		} 
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		
		return weiboList;
	}
	
	public int parseEventWeiboPageCount(String weiboPage)
	{
		try
		{
			JSONObject jsonObject = new JSONObject(weiboPage);
			String weiboInfo = jsonObject.getString("data");
			Document doc = Jsoup.parse(weiboInfo);
			//System.out.println(doc.toString());
			
			int pageCount = 1;
			Elements pageElements = doc.getElementsByAttributeValue("action-type", "feed_list_page_morelist");
			if(pageElements != null && pageElements.size() > 0)
				pageCount = pageElements.get(0).getElementsByTag("li").size();
			return pageCount;
			//System.out.println(doc.toString());
		} 
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return 1;
	}
	
	/**
	 * 解析事件列表
	 * @param eventPage
	 * @return
	 */
	public List<EventBean> parseEventList(String eventPage)
	{
		//System.out.println(eventPage);
		
		if(eventPage == null || eventPage.length() < 10)
			return null;
		
		Document htmlDoc = Jsoup.parse(eventPage);
		Elements scriptElements = htmlDoc.getElementsByTag("script");
		if(scriptElements == null)
			return null;
		
		String eventStr = null;
		for(Element se : scriptElements)
		{
			String currentStr = se.toString().trim();
			if(currentStr.contains("FM.view({\"ns\":\"pl.content.miniTab.index\",\"domid\":\"Pl_Discover_Pt6Rank__"))
			{	
				eventStr = currentStr;
				break;
			}
		}
		
		if(eventStr == null)
			return null;
		
		List<EventBean> eventList = new ArrayList<EventBean>();
		try
		{
			
			eventStr = eventStr.substring(eventStr.indexOf('(') + 1, eventStr.lastIndexOf(')'));
			JSONObject jsonObject;
			jsonObject = new JSONObject(eventStr);
			String userInfoHtml = jsonObject.getString("html");
			htmlDoc = Jsoup.parse(userInfoHtml);
			
			Elements ees = htmlDoc.getElementsByAttributeValue("class", "pt_li S_line2");
			for(Element e : ees)
			{
				EventBean eb = parseEventInfo(e);
				if(eb != null)
					eventList.add(eb);
			}
			
			return eventList;
			
			//System.out.println(htmlDoc.toString());
			
		}
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return eventList;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return eventList;
		}
	}
	
	/**
	 * 解析事件信息
	 * @param e 包含事件信息元素
	 * @return
	 */
	public EventBean parseEventInfo(Element e)
	{
		try
		{
			EventBean eb = new EventBean();
			
			Element infoElement = e.getElementsByAttributeValue("class", "title W_autocut").get(0);
			Elements tagElements = infoElement.getElementsByTag("a");
			
			String eventName = e.getElementsByClass("pic").get(0).attr("alt").trim().substring(1);
			eventName = eventName.substring(0, eventName.lastIndexOf("#"));
			String url = tagElements.get(0).attr("href");
			String eventCode = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('?'));
			String type = "";
			if(tagElements.size() > 1)
				type = tagElements.get(1).text().trim();
			String description = e.getElementsByAttributeValue("class", "subtitle").get(0).text().trim();
			String host = "";
			if(e.getElementsByAttributeValue("class", "tlink S_txt1") != null && e.getElementsByAttributeValue("class", "tlink S_txt1").size() > 0)
				host = e.getElementsByAttributeValue("class", "tlink S_txt1").get(0).text().trim();
			
			eb.setHostPerson(host);
			eb.setEventname(eventName);
			eb.setUrl(url);
			eb.setEventCode(eventCode);
			eb.setType(type);
			eb.setDescription(description);
			
			
			return eb;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return null;
		}
	}
	
	
	
	/**
	 * 从HTML元素中解析出一条微博
	 * @param weiboElement
	 * @param ub 用户信息
	 * @return 一条微博对象
	 */
	public WeiboBean parseWeibo(Element weiboElement, UserBean ub)
	{
		String feedListStr = "fl";
		if(ub != null)
			feedListStr = "feed_list";
		
		if(ub == null)
		{
			ub = new UserBean();
			String uid = weiboElement.attr("tbinfo");
			uid = uid.substring("ouid=".length());
			if(uid.contains("&"))
				uid = uid.substring(0, uid.indexOf('&'));
			ub.setUid(uid);
			
			String nickName = weiboElement.getElementsByClass("W_face_radius").get(0).attr("title").trim();
			ub.setNickName(nickName);
			
		}
		
		WeiboBean wb = new WeiboBean();
		
		
		String mid = weiboElement.attr("mid");
		String isforward = weiboElement.attr("isforward");
		String weiboText = "";
		if(weiboElement.getElementsByAttributeValue("node-type", "feed_list_content") != null && weiboElement.getElementsByAttributeValue("node-type", "feed_list_content").size() > 0)
			weiboText = weiboElement.getElementsByAttributeValue("node-type", "feed_list_content").get(0).ownText();
		String mapData = "";
		if(weiboElement.getElementsByClass("map_data") != null && weiboElement.getElementsByClass("map_data").size() != 0)
			mapData = weiboElement.getElementsByClass("map_data").get(0).text();
		
		String likeFlag = String.format("%s_like", feedListStr);
		String commentFlag = String.format("%s_comment", feedListStr);
		String forwardFlag = String.format("%s_forward", feedListStr);
		String likeStr = "";
		if(weiboElement.getElementsByAttributeValue("action-type", likeFlag) != null && weiboElement.getElementsByAttributeValue("action-type", likeFlag).size() >= 1)
			likeStr = weiboElement.getElementsByAttributeValue("action-type", likeFlag).get(0).getElementsByTag("em").get(0).text().trim();
		int like = 0;
		
			//Integer.parseInt(likeStr.substring(likeStr.indexOf('(') + 1, likeStr.indexOf(")")));
		likeStr = likeStr.replace("赞", "");
		if(likeStr.contains("("))
			like = Integer.parseInt(likeStr.substring(likeStr.indexOf('(') + 1, likeStr.indexOf(")")));
		else if(likeStr.length() > 0)
			like = Integer.parseInt(likeStr);
		
		String forwardStr = "";
		if(weiboElement.getElementsByAttributeValue("action-type", forwardFlag).size() > 0)
			forwardStr = weiboElement.getElementsByAttributeValue("action-type", forwardFlag).get(0).text();
		int forward = 0;
		forwardStr = forwardStr.replace("转发", "");
		if(forwardStr.contains("("))
			forward = Integer.parseInt(forwardStr.substring(forwardStr.indexOf('(') + 1, forwardStr.indexOf(')')));
		else if(forwardStr.length() > 0)
		{
			forwardStr = forwardStr.replace("转发", "").replace("", "").trim();
			if(forwardStr.length() > 0)
				forward = Integer.parseInt(forwardStr);
			
			//Integer.parseInt(forwardStr.substring(forwardStr.indexOf('(') + 1, forwardStr.indexOf(')')));
		}
		
		String commentStr = "";
		if(weiboElement.getElementsByAttributeValue("action-type", commentFlag).size() > 0)
			commentStr = weiboElement.getElementsByAttributeValue("action-type", commentFlag).get(0).text();
		int comment = 0;
		if(commentStr.contains("("))
			comment = Integer.parseInt(commentStr.substring(commentStr.indexOf('(') + 1, commentStr.indexOf(')')));
		else if(commentStr.length() > 0)
		{
			commentStr = commentStr.replace("评论", "").trim();
			commentStr = commentStr.replace("", "").trim();
			if(commentStr.length() > 0)
				comment = Integer.parseInt(commentStr);
			//Integer.parseInt(commentStr.substring(commentStr.indexOf('(') + 1, commentStr.indexOf(')')));
		}
		
		String from = "";
		if(weiboElement.getElementsByAttributeValue("action-type", "app_source") != null && weiboElement.getElementsByAttributeValue("action-type", "app_source").size() > 0)
			from = weiboElement.getElementsByAttributeValue("action-type", "app_source").get(0).text();
		String datetime = "";
		if(weiboElement.getElementsByAttributeValue("node-type", "feed_list_item_date") != null && weiboElement.getElementsByAttributeValue("node-type", "feed_list_item_date").size() > 0)
			datetime = weiboElement.getElementsByAttributeValue("node-type", "feed_list_item_date").get(0).attr("title");
		String url = String.format(SpiderConfig.getWeiboUrl(), ub.getUid(), SinaSSOEncoder.getSinamid(mid));
		
		wb.setNickname(ub.getNickName());
		wb.setUrl(url);
		wb.setTime(datetime);
		wb.setFrom(from);
		wb.setUid(ub.getUid());
		wb.setMid(mid);
		wb.setContent(weiboText);
		wb.setMapData(mapData);
		wb.setLikeNum(like);
		wb.setCommentNum(comment);
		wb.setForwardNum(forward);
		//添加转发信息
		if("1".equals(isforward))
		{
			try
			{
				wb.setIsforward(true);
				if(weiboElement.getElementsByAttributeValue("action-type", likeFlag).size() >= 2)
					likeStr = weiboElement.getElementsByAttributeValue("action-type", likeFlag).get(1).text();
				else
					likeStr = "";
				like = 0;
				likeStr = likeStr.replace("赞", "");
				if(likeStr.contains("("))
					like = Integer.parseInt(likeStr.substring(likeStr.indexOf('(') + 1, likeStr.indexOf(")")));
				else if(likeStr.length() > 0)
					like = Integer.parseInt(likeStr);
				String forwardContent = "";
				if(weiboElement.getElementsByAttributeValue("node-type","feed_list_reason") != null && weiboElement.getElementsByAttributeValue("node-type","feed_list_reason").size() > 0)
					forwardContent = weiboElement.getElementsByAttributeValue("node-type","feed_list_reason").get(0).text();
				String forwardUid = weiboElement.attr("tbinfo");
				forwardUid = forwardUid.substring(forwardUid.indexOf("rouid=") + "rouid=".length());
				
				wb.setForwardFrom(wb.getFrom());
				if(weiboElement.getElementsByAttributeValue("action-type", "app_source").size() < 2)
				{
					from = wb.getFrom();
					wb.setForwardFrom("");
				}
				else
					from = weiboElement.getElementsByAttributeValue("action-type", "app_source").get(1).text();
				String forwardNickname = "";
				if(weiboElement.getElementsByAttributeValue("node-type", "feed_list_originNick") != null && weiboElement.getElementsByAttributeValue("node-type", "feed_list_originNick").size() > 0)
					forwardNickname = weiboElement.getElementsByAttributeValue("node-type", "feed_list_originNick").get(0).attr("nick-name");
				if(weiboElement.getElementsByAttributeValue("node-type", "feed_list_item_date") != null && weiboElement.getElementsByAttributeValue("node-type", "feed_list_item_date").size() > 1)
					datetime = weiboElement.getElementsByAttributeValue("node-type", "feed_list_item_date").get(1).attr("title");
				
				wb.setTime(datetime);
				wb.setFrom(from);
				wb.setForwardMid(weiboElement.attr("omid"));
				wb.setForwardUid(forwardUid);
				wb.setForwardContent(forwardContent);
				wb.setForwardNickname(forwardNickname);
				wb.setLikeNum(like);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			
		}
		
		return wb;
	}
	
	/**
	 * 解析用户微博
	 * @param weiboElement
	 * @param ub
	 * @return
	 */
	public WeiboBean parseUserWeibo(Element weiboElement, UserBean ub)
	{
		WeiboBean wb = new WeiboBean();
		
		
		String mid = weiboElement.attr("mid");
		String isforward = weiboElement.attr("isforward");
		String weiboText = weiboElement.getElementsByClass("WB_text").get(0).ownText();
		String mapData = "";
		if(weiboElement.getElementsByClass("map_data") != null && weiboElement.getElementsByClass("map_data").size() != 0)
			mapData = weiboElement.getElementsByClass("map_data").get(0).text();
		String likeStr = "";
		if(weiboElement.getElementsByAttributeValue("action-type", "feed_list_like") != null && weiboElement.getElementsByAttributeValue("action-type", "feed_list_like").size() > 1)
			likeStr = weiboElement.getElementsByAttributeValue("action-type", "feed_list_like").get(0).text();
		int like = 0;
		if(likeStr.contains("("))
			like = Integer.parseInt(likeStr.substring(likeStr.indexOf('(') + 1, likeStr.indexOf(")")));
		String forwardStr = "";
		if(weiboElement.getElementsByAttributeValue("action-type", "feed_list_forward").size() > 0)
			forwardStr = weiboElement.getElementsByAttributeValue("action-type", "feed_list_forward").get(0).text();
		int forward = 0;
		if(forwardStr.contains("("))
			forward = Integer.parseInt(forwardStr.substring(forwardStr.indexOf('(') + 1, forwardStr.indexOf(')')));
		String commentStr = "";
		if(weiboElement.getElementsByAttributeValue("action-type", "feed_list_comment").size() > 0)
			commentStr = weiboElement.getElementsByAttributeValue("action-type", "feed_list_comment").get(0).text();
		int comment = 0;
		if(commentStr.contains("("))
			comment = Integer.parseInt(commentStr.substring(commentStr.indexOf('(') + 1, commentStr.indexOf(')')));
		String from = weiboElement.getElementsByAttributeValue("action-type", "app_source").get(0).text();
		String datetime = weiboElement.getElementsByAttributeValue("class", "S_link2 WB_time").get(0).attr("title");
		String url = String.format(SpiderConfig.getWeiboUrl(), ub.getUid(), SinaSSOEncoder.getSinamid(mid));
		
		wb.setNickname(ub.getNickName());
		wb.setUrl(url);
		wb.setTime(datetime);
		wb.setFrom(from);
		wb.setUid(ub.getUid());
		wb.setMid(mid);
		wb.setContent(weiboText);
		wb.setMapData(mapData);
		wb.setLikeNum(like);
		wb.setCommentNum(comment);
		wb.setForwardNum(forward);
		//添加转发信息
		if("1".equals(isforward))
		{
			try
			{
				wb.setIsforward(true);
				if(weiboElement.getElementsByAttributeValue("action-type", "feed_list_like").size() >= 2)
					likeStr = weiboElement.getElementsByAttributeValue("action-type", "feed_list_like").get(1).text();
				else
					likeStr = "";
				like = 0;
				if(likeStr.contains("("))
					like = Integer.parseInt(likeStr.substring(likeStr.indexOf('(') + 1, likeStr.indexOf(")")));
				String forwardContent = "";
				if(weiboElement.getElementsByClass("WB_text").size() >= 2)
					forwardContent = weiboElement.getElementsByClass("WB_text").get(1).ownText();
				String forwardUid = weiboElement.attr("tbinfo");
				forwardUid = forwardUid.substring(forwardUid.indexOf("rouid=") + "rouid=".length());
				
				wb.setForwardFrom(wb.getFrom());
				if(weiboElement.getElementsByAttributeValue("action-type", "app_source").size() < 2)
				{
					from = wb.getFrom();
					wb.setForwardFrom("");
				}
				else
					from = weiboElement.getElementsByAttributeValue("action-type", "app_source").get(1).text();
				
				wb.setFrom(from);
				wb.setForwardMid(weiboElement.attr("omid"));
				wb.setForwardUid(forwardUid);
				wb.setForwardContent(forwardContent);
				wb.setLikeNum(like);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			
		}
		
		return wb;
	}
	
	/**
	 * 解析用户信息
	 * @param userInfoPage
	 * @return 用户对象
	 */
	public UserBean parseUserInfo(String userInfoPage)
	{
		UserBean ub = new UserBean();
		try
		{
			Document userInfoDoc = Jsoup.parse(userInfoPage);
			Elements scriptElements = userInfoDoc.getElementsByTag("script");
			//System.out.println(userInfoDoc.toString());
			String userInfo = null;
			String header = null;
			String countInfo = null;
			Element currentScript = null;
			String currentInfo = null;
			int i = 0;
			for(i = 0; i < scriptElements.size(); i++)
			{
				currentScript = scriptElements.get(i);
				currentInfo = currentScript.toString().trim();
				if(currentInfo.contains("FM.view({\"ns\":\"\",\"domid\":\"Pl_Official_PersonalInfo"))
					userInfo = currentInfo;
				else if(currentInfo.contains("FM.view({\"ns\":\"pl.header.head.index\",\"domid"))
					header = currentInfo;
				else if(currentInfo.contains("FM.view({\"ns\":\"\",\"domid\":\"Pl_Core_T8CustomTriColumn__"))
					countInfo = currentInfo;
			}
			
			//处理个人信息
			if(userInfo != null && userInfo.length() > 0)
				parsePersonalInfo(ub, userInfo);
			//处理头部信息
			if(header != null && header.length() > 0)
				parseUserHeader(ub, header);
			//处理计数信息，微博粉丝数等等
			if(countInfo != null && countInfo.length() > 0)
				parseCountInfo(ub, countInfo);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return ub;
	}
	
	/**
	 * 解析用户的计数信息（微博数，粉丝数等）
	 * @param ub
	 * @param countInfo
	 */
	public void parseCountInfo(UserBean ub, String countInfo)
	{
		if(ub == null)
			ub = new UserBean();
		
		try
		{
			countInfo = countInfo.substring(countInfo.indexOf('(') + 1, countInfo.lastIndexOf(')'));
			JSONObject jsonObject;
			
			jsonObject = new JSONObject(countInfo);
			String userInfoHtml = jsonObject.getString("html");
			Document htmlDoc = Jsoup.parse(userInfoHtml);
			
			int weibo = 0;
			int fans = 0;
			int focus = 0;
			
			Elements countElements = htmlDoc.getElementsByClass("W_f18");
			if(countElements == null || countElements.size() == 0)
				countElements = htmlDoc.getElementsByClass("W_f12");
			if(countElements == null || countElements.size() == 0)
				countElements = htmlDoc.getElementsByClass("W_f16");
			if(countElements == null || countElements.size() == 0)
				countElements = htmlDoc.getElementsByClass("W_f14");
			focus = Integer.parseInt(countElements.get(0).text().trim());
			fans = Integer.parseInt(countElements.get(1).text().trim());
			weibo = Integer.parseInt(countElements.get(2).text().trim());
			
			ub.setFans(fans);
			ub.setFollow(focus);
			ub.setWeibo(weibo);
			
		}
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}
	
	
	/**
	 * 解析个人信息
	 * @param ub
	 * @param personalInfo
	 */
	public void parsePersonalInfo(UserBean ub, String userInfo)
	{
		if(ub == null)
			ub = new UserBean();
		try
		{
			userInfo = userInfo.substring(userInfo.indexOf('(') + 1, userInfo.lastIndexOf(')'));
			JSONObject jsonObject = new JSONObject(userInfo);
			String userInfoHtml = jsonObject.getString("html");
			Document htmlDoc = Jsoup.parse(userInfoHtml);
			Elements infoItems = htmlDoc.getElementsByAttributeValue("class", "li_1 clearfix");
			
			Map<String, String> itemValues = new HashMap<String, String>();
			for(Element currentElement : infoItems)
			{
				String key = currentElement.getElementsByAttributeValue("class", "pt_title S_txt2").get(0).text();
				key = key.replace("：", "").trim();
				key = key.replace(" ", "");
				String value = "";
				
				if(key.equals("标签"))
				{
					Element valueElement = currentElement.getElementsByAttributeValue("class", "pt_detail").get(0);
					Elements tagElements = valueElement.getElementsByTag("a");
					for(Element cve : tagElements)
						value = value + cve.text() + "\t";
					value = value.substring(0, value.lastIndexOf('\t'));
				}
				else
				{
					value = currentElement.text();
					value = value.replace(key + "：", "");
				}
				itemValues.put(key, value);
			}
			
			ub.setNickName(itemValues.get("昵称"));
			ub.setSex(itemValues.get("性别"));
			ub.setLocation(itemValues.get("所在地"));
			ub.setSexOrientation(itemValues.get("性取向"));
			ub.setMaritalStatus(itemValues.get("感情状况"));
			ub.setBirth(itemValues.get("生日"));
			ub.setCreateTime(itemValues.get("注册时间"));
			ub.setEmail(itemValues.get("邮箱"));
			ub.setQq(itemValues.get("QQ"));
			ub.setMsn(itemValues.get("MSN"));
			ub.setBlog(itemValues.get("博客"));
			ub.setSite(itemValues.get("个性域名"));
			ub.setIntroduction(itemValues.get("简介"));
			ub.setCompany(itemValues.get("公司"));
			ub.setTags(itemValues.get("标签"));
			ub.setBloodType(itemValues.get("血型"));
			ub.setEducationInfo(itemValues.get("大学"));

		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 解析用户头个人信息
	 * @param ub
	 */
	public void parseUserHeader(UserBean ub, String header)
	{
		if(ub == null)
			ub = new UserBean();
		try
		{
			header = header.substring(header.indexOf('(') + 1, header.lastIndexOf(')'));
			JSONObject jsonObject = new JSONObject(header);
			header = jsonObject.getString("html");
			Document headerDoc = Jsoup.parse(header);
			
			String approvelInfo = "";
			String headPicture = "";
			boolean isMember = false;
			boolean isApprovel = false;
			Element headElement = headerDoc.getElementsByTag("img").get(0);
			headPicture = headElement.attr("src");
			if(headerDoc.getElementsByAttributeValue("title", "微博会员") != null)
				isMember = true;
			String intro = "";
			if(ub.getIntroduction().equals("") || ub.getIntroduction() == null)
			{
				intro = headerDoc.getElementsByAttributeValue("class", "pf_intro").get(0).attr("title");
				ub.setIntroduction(intro);
			}
			
			if(headerDoc.getElementsByAttributeValue("class", "W_icon icon_pf_approve") != null && headerDoc.getElementsByAttributeValue("class", "W_icon icon_pf_approve").size() > 0)
			{
				isApprovel = true;
				Element approvelElement = headerDoc.getElementsByAttributeValue("class", "W_icon icon_pf_approve").get(0);
				approvelInfo = approvelElement.attr("title").trim();
			}
			//官方用户
			if(headerDoc.getElementsByAttributeValue("class", "W_icon icon_pf_approve_co") != null && headerDoc.getElementsByAttributeValue("class", "W_icon icon_pf_approve_co").size() > 0)
			{
				ub.setOfficial(true);
				isApprovel = true;
				Element approvelElement = headerDoc.getElementsByAttributeValue("class", "W_icon icon_pf_approve_co").get(0);
				approvelInfo = approvelElement.attr("title").trim();
			}
			
			ub.setApproved(isApprovel);
			ub.setApproveInfo(approvelInfo);
			ub.setMember(isMember);
			ub.setHeadPicture(headPicture);
			
			/*
			int follow = 0;
			int fans = 0;
			int weibo = 0;
			try
			{
				follow  = Integer.parseInt(headerDoc.getElementsByAttributeValue("node-type", "follow").get(0).text());
				fans = Integer.parseInt(headerDoc.getElementsByAttributeValue("node-type", "fans").get(0).text());
				weibo = Integer.parseInt(headerDoc.getElementsByAttributeValue("node-type", "weibo").get(0).text());
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			
			String approvInfo = "";
			Elements approveElements = headerDoc.getElementsByAttributeValue("class", "pf_verified_info bsp S_txt2");
			int size = approveElements.size();
			if(approveElements != null && size != 0)
			{
				approvInfo = approveElements.get(0).ownText();
			}
			boolean isMember = false;
			if(headerDoc.getElementsByAttributeValue("title", "微博会员") != null)
				isMember = true;
			int level = Integer.parseInt(headerDoc.getElementsByAttribute("levelcard").get(0).attr("title").replace("当前等级：", ""));
			String headerPic = "";
			Elements headerPicDivs = headerDoc.getElementsByAttributeValue("class", "pf_head_pic");
			if(headerPicDivs != null && headerPicDivs.size() > 0)
				headerPic = headerPicDivs.get(0).getElementsByTag("img").get(0).attr("src");
			
			ub.setHeadPicture(headerPic);
			ub.setFollow(follow);
			ub.setFans(fans);
			if(!approvInfo.equals(""))
				ub.setApproved(true);
			ub.setApproveInfo(approvInfo);
			ub.setMember(isMember);
			ub.setLevel(level);
			ub.setWeibo(weibo);
			
			*/
			//System.out.println(headerDoc.toString());
		} 
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}
	
	public List<WeiboBean> parseFirstEventWeibo(String weiboPage)
	{
		Document doc = Jsoup.parse(weiboPage);
		Elements ses = doc.getElementsByTag("script");
		
		
		try
		{
			String hotInfo = null;
			
			String domid = SpiderConfig.getDomid();
			for(Element weiboElement : ses)
			{
				String currentStr = weiboElement.toString();
				if(currentStr.contains("FM.view({\"ns\":\"pl.content.homeFeed.index\",\"domid\":\"" + domid))
					hotInfo = currentStr;
				//else if(currentStr.contains("FM.view({\"ns\":\"pl.content.homeFeed.index\",\"domid\":\"Pl_Third_App__7"))
				//	weiboInfo = currentStr;
			}
			
			Elements weiboElements = new Elements();
			/*
			if(weiboInfo != null)
			{
				weiboInfo = weiboInfo.substring(weiboInfo.indexOf('(') + 1, weiboInfo.lastIndexOf(')'));
				JSONObject jsonObject;
				
				jsonObject = new JSONObject(weiboInfo);
				weiboInfo = jsonObject.getString("html");
				Document headerDoc = Jsoup.parse(weiboInfo);
				
				weiboElements = headerDoc.getElementsByAttributeValue("action-type", "feed_list_item");
			}
			*/
			if(hotInfo != null)
			{
				hotInfo = hotInfo.substring(hotInfo.indexOf('(') + 1, hotInfo.lastIndexOf(')'));
				JSONObject jsonObject;
				
				jsonObject = new JSONObject(hotInfo);
				hotInfo = jsonObject.getString("html");
				Document headerDoc = Jsoup.parse(hotInfo);
				
				weiboElements.addAll(headerDoc.getElementsByAttributeValue("action-type", "feed_list_item"));

			}
			
			List<WeiboBean> wbList = new ArrayList<WeiboBean>();
			for(Element e : weiboElements)
			{
				wbList.add(parseWeibo(e, null));
			}
			return wbList;
			
			
		} 
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
		
	}
	
	/**
	 * 解析事件的热门微博
	 * @param weiboPage
	 * @return
	 */
	public List<WeiboBean> parseEventHotWeibo(String weiboPage)
	{
		Document doc = Jsoup.parse(weiboPage);
		Elements ses = doc.getElementsByTag("script");
		
		//System.out.println(doc.toString());
		
		try
		{
			String weiboInfo = null;
			
			for(Element weiboElement : ses)
			{
				String currentStr = weiboElement.toString();
				//String test =          "FM.view({\"ns\":\"pl.content.homeFeed.index\",\"domid\":\"Pl_Third_App__7";
				if(currentStr.contains("FM.view({\"ns\":\"pl.content.homeFeed.index\",\"domid\":\"Pl_Third_App__8"))
					weiboInfo = currentStr;
			}
			
			
			Elements weiboElements = null;
			if(weiboInfo != null)
			{
				weiboInfo = weiboInfo.substring(weiboInfo.indexOf('(') + 1, weiboInfo.lastIndexOf(')'));
				JSONObject jsonObject;
				
				jsonObject = new JSONObject(weiboInfo);
				weiboInfo = jsonObject.getString("html");
				Document headerDoc = Jsoup.parse(weiboInfo);
				
				weiboElements = headerDoc.getElementsByAttributeValue("action-type", "feed_list_item");
			}
			List<WeiboBean> wbList = new ArrayList<WeiboBean>();
			if(weiboElements != null)
			{
				for(Element e : weiboElements)
				{
					wbList.add(parseWeibo(e, null));
				}
			}
			return wbList;
			
			
		} 
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
		
	}
	
	/**
	 * 解析名人页数
	 * @param responseStr
	 * @return
	 */
	public int parseFamousPersonPageCount(String responseStr)
	{
		Document doc = Jsoup.parse(responseStr);
		
		Elements scriptElements = doc.getElementsByTag("script");
		int pageCount = 1;
		String usefulStr = "";
		
		for(Element e : scriptElements)
		{
			String currentStr = e.toString().trim();
			if(currentStr.contains("pl.content.signInPeople.index"))
			{
				usefulStr = currentStr;
				break;
			}
			
		}
		
		usefulStr = usefulStr.substring(usefulStr.indexOf('(') + 1, usefulStr.lastIndexOf(')'));
		JSONObject jsonObject;
		
		try
		{
			jsonObject = new JSONObject(usefulStr);
			usefulStr = jsonObject.getString("html");
			Document headerDoc = Jsoup.parse(usefulStr);
			Elements pageElements = headerDoc.getElementsByTag("a");
			
			String pageStr = pageElements.get(pageElements.size() - 2).text().trim();
			
			pageCount = Integer.parseInt(pageStr);
			
		} catch (JSONException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		
		return pageCount;
	}
	
	/**
	 * 解析名人用户ID
	 * @param htmlStr
	 * @return
	 */
	public List<String> parseFamousPersonUids(String htmlStr)
	{
		List<String> uidList = new ArrayList<String>();
		
		Document doc = Jsoup.parse(htmlStr);
		
		Elements scriptElements = doc.getElementsByTag("script");
		String usefulStr = "";
		
		for(Element e : scriptElements)
		{
			String currentStr = e.toString().trim();
			if(currentStr.contains("pl.content.signInPeople.index"))
			{
				usefulStr = currentStr;
				break;
			}
			
		}
		
		usefulStr = usefulStr.substring(usefulStr.indexOf('(') + 1, usefulStr.lastIndexOf(')'));
		JSONObject jsonObject;
		
		try
		{
			jsonObject = new JSONObject(usefulStr);
			usefulStr = jsonObject.getString("html");
			Document headerDoc = Jsoup.parse(usefulStr);
			Elements userElements = headerDoc.getElementsByAttributeValue("class", "follow_item S_line2");
			
			for(Element e : userElements)
			{
				String uid = e.getElementsByTag("img").get(0).attr("usercard");
				uid = uid.replace("id=", "").trim();
				uidList.add(uid);
			}
			
			
					
			
		} catch (JSONException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		return uidList;
		
	}
	
	public String getHtmlTitle(String htmlStr)
	{
		Document doc = Jsoup.parse(htmlStr);
		String title = "";
		Elements es = doc.getElementsByTag("title");
		if(es != null && es.size() > 0)
			title = es.get(0).text().trim();
		return title;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
